package main

import (
	"fmt"
	"gitee.com/freelifer/module-admin/model"
	_ "gitee.com/freelifer/module-admin/services"
	_ "gitee.com/freelifer/starter-gorm/sqlite"
	"github.com/go-spring/spring-base/log"
	"github.com/go-spring/spring-core/gs"
	"github.com/pkg/errors"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
)

type runner struct {
	DB *gorm.DB `autowire:""`
}

func (r *runner) Run(ctx gs.Context) {
	var engines []string
	r.DB.Raw("select engine from engines").Scan(&engines)
	log.Infof("got mysql engines %v", engines)

	fmt.Println("InitApplication OnStartApplication")
	r.DB.Debug()
	err := r.DB.AutoMigrate(&model.SysApi{}, &model.SysRole{}, &model.SysUser{})
	if err != nil {
		fmt.Println(err)
	}
	err = r.InitializeSysAPI()
	if err != nil {
		fmt.Println(err)
	}
	err = r.InitializeSysRole()
	if err != nil {
		fmt.Println(err)
	}
	err = r.InitializeSysUser()
	if err != nil {
		fmt.Println(err)
	}

	go gs.ShutDown()
}

// go build -ldflags "-s -w"
// upx main
func main() {
	gs.Object(&runner{}).Export((*gs.AppRunner)(nil))
	fmt.Printf("program exited %v\n", gs.Web(false).Run())
}

func (starter *runner) InitializeSysAPI() error {
	entities := []model.SysApi{
		{ApiGroup: "base", Method: "POST", Path: "/base/login", Description: "用户登录(必选)"},

		{ApiGroup: "jwt", Method: "POST", Path: "/jwt/jsonInBlacklist", Description: "jwt加入黑名单(退出，必选)"},

		{ApiGroup: "系统用户", Method: "DELETE", Path: "/user/deleteUser", Description: "删除用户"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/register", Description: "用户注册(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/getUserList", Description: "获取用户列表"},
		{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setUserInfo", Description: "设置用户信息"},
		{ApiGroup: "系统用户", Method: "PUT", Path: "/user/setSelfInfo", Description: "设置自身信息(必选)"},
		{ApiGroup: "系统用户", Method: "GET", Path: "/user/getUserInfo", Description: "获取自身信息(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthorities", Description: "设置权限组"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/changePassword", Description: "修改密码（建(选择)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthority", Description: "修改用户角色(必选)"},
		{ApiGroup: "系统用户", Method: "POST", Path: "/user/resetPassword", Description: "重置用户密码"},

		{ApiGroup: "api", Method: "POST", Path: "/api/createApi", Description: "创建api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/deleteApi", Description: "删除Api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/updateApi", Description: "更新Api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getApiList", Description: "获取api列表"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getAllApis", Description: "获取所有api"},
		{ApiGroup: "api", Method: "POST", Path: "/api/getApiById", Description: "获取api详细信息"},
		{ApiGroup: "api", Method: "DELETE", Path: "/api/deleteApisByIds", Description: "批量删除api"},

		{ApiGroup: "角色", Method: "POST", Path: "/authority/copyAuthority", Description: "拷贝角色"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/createAuthority", Description: "创建角色"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/deleteAuthority", Description: "删除角色"},
		{ApiGroup: "角色", Method: "PUT", Path: "/authority/updateAuthority", Description: "更新角色信息"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/getAuthorityList", Description: "获取角色列表"},
		{ApiGroup: "角色", Method: "POST", Path: "/authority/setDataAuthority", Description: "设置角色资源权限"},

		{ApiGroup: "casbin", Method: "POST", Path: "/casbin/updateCasbin", Description: "更改角色api权限"},
		{ApiGroup: "casbin", Method: "POST", Path: "/casbin/getPolicyPathByAuthorityId", Description: "获取权限列表"},

		{ApiGroup: "菜单", Method: "POST", Path: "/menu/addBaseMenu", Description: "新增菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenu", Description: "获取菜单树(必选)"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/deleteBaseMenu", Description: "删除菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/updateBaseMenu", Description: "更新菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuById", Description: "根据id获取菜单"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuList", Description: "分页获取基础menu列表"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuTree", Description: "获取用户动态路由"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/getMenuAuthority", Description: "获取指定角色menu"},
		{ApiGroup: "菜单", Method: "POST", Path: "/menu/addMenuAuthority", Description: "增加menu和角色关联关系"},
	}
	fmt.Printf("sysAPI表导入数据条数：%d\n", len(entities))
	if err := starter.DB.Create(&entities).Error; err != nil {
		return errors.Wrap(err, "sysAPI表数据初始化失败!")
	}
	return nil
}
func (starter *runner) InitializeSysRole() error {
	entities := []model.SysRole{
		{RoleId: "888", RoleName: "普通用户", ParentId: "0", DefaultRouter: "dashboard"},
		//{RoleId: "9528", RoleName: "测试角色", ParentId: "0", DefaultRouter: "dashboard"},
		//{RoleId: "8881", RoleName: "普通用户子角色", ParentId: "888", DefaultRouter: "dashboard"},
	}
	fmt.Printf("SysRole表导入数据条数：%d\n", len(entities))
	if err := starter.DB.Create(&entities).Error; err != nil {
		return errors.Wrap(err, "SysRole表数据初始化失败!")
	}
	return nil
}
func (starter *runner) InitializeSysUser() error {
	entities := []model.SysUser{
		{UUID: uuid.NewV4(), Username: "admin", Password: "e10adc3949ba59abbe56e057f20f883e", NickName: "超级管理员", HeaderImg: "https://qmplusimg.henrongyi.top/gva_header.jpg", RoleId: "888"},
		//{UUID: uuid.NewV4(), Username: "a303176530", Password: "3ec063004a6f31642261936a379fde3d", NickName: "QMPlusUser", HeaderImg: "https:///qmplusimg.henrongyi.top/1572075907logo.png", RoleId: "9528"},
	}
	fmt.Printf("SysUser表导入数据条数：%d\n", len(entities))
	if err := starter.DB.Create(&entities).Error; err != nil {
		return errors.Wrap(err, "SysUser表数据初始化失败!")
	}
	return nil
}
